//
//  DataManager.m
//  DriveStudy
//
//  Created by chen on 2017/4/13.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "DataManager.h"

@implementation DataManager

+(NSArray *)getData:(DataType)type{
    static FMDatabase *database;
    NSMutableArray *array = [[NSMutableArray alloc]init];
    if (database == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data.sqlite" ofType:nil];
        database = [FMDatabase databaseWithPath:path];
    }
    if ([database open]) {
        NSLog(@"数据库打开成功！");
    }else{
        NSLog(@"数据库打开不成功！");
        return array;
    }
    switch (type) {
        case chapterTest:
        {
            NSString *sql = @"select * from firstlevel";
            FMResultSet *rs = [database executeQuery:sql];
            while ([rs next]) {
                FirstLevelModel *first = [FirstLevelModel new];
                first.serial = [NSString stringWithFormat:@"%d",[rs intForColumn:@"serial"]];
                first.pid = [NSString stringWithFormat:@"%d",[rs intForColumn:@"pid"]];
                first.pname = [rs stringForColumn:@"pname"];
                first.pcount =[NSString stringWithFormat:@"%d",[rs intForColumn:@"pcount"]];
                [array addObject:first];
            }
        }
            break;
        case answer:
        {
            NSString *sql = @"select mquestion,mdesc,mid,manswer,mimage,manswer,pname,pid,sid,sname,mtype from leaflevel";
            FMResultSet *rs = [database executeQuery:sql];
            while ([rs next]) {
                AnswerModel *answer = [AnswerModel new];
                answer.mquestion = [rs stringForColumn:@"mquestion"];
                answer.mdesc = [rs stringForColumn:@"mdesc"];
                answer.manswer = [rs stringForColumn:@"manswer"];
                answer.mpname = [rs stringForColumn:@"pname"];
                answer.sname = [rs stringForColumn:@"sname"];
                answer.mimage = [rs stringForColumn:@"mimage"];
                answer.mid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"mid"]];
                answer.pid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pid"]];
                answer.sid = [rs stringForColumn:@"sid"];
                answer.mtype = [NSString stringWithFormat:@"%d", [rs intForColumn:@"mtype"]];
                [array addObject:answer];
            }
        }
            break;
        case specialTest:
        {
            NSString *sql = @"select * from secondlevel";
            FMResultSet *rs = [database executeQuery:sql];
            while ([rs next]) {
                SecondLevelModel *second = [SecondLevelModel new];
                second.serial =[NSString stringWithFormat:@"%d", [rs intForColumn:@"serial"]];
                second.sid = [rs stringForColumn:@"sid"];
                second.sname = [rs stringForColumn:@"sname"];
                second.pid = [NSString stringWithFormat:@"%d", [rs intForColumn:@"pid"]];
                second.scount =[NSString stringWithFormat:@"%d", [rs intForColumn:@"scount"]];
                [array addObject:second];
            }
        }
            break;
        case emulateTest:
        {
            NSString *sql = @"select * from firstlevel";
            FMResultSet *rs = [database executeQuery:sql];
            while ([rs next]) {
                FirstLevelModel *first = [FirstLevelModel new];
                first.serial = [rs stringForColumn:@"serial"];
                first.pid = [rs stringForColumn:@"pid"];
                first.pname = [rs stringForColumn:@"pname"];
                first.pcount = [rs stringForColumn:@"pcout"];
                [array addObject:first];
            }
        }
            break;
        
        default:
            break;
    }
    return array;
}
@end
